package com.zou.lotterylucky.adapters

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zou.lotterylucky.R
import com.zou.lotterylucky.data.WinningHistory

class HistoryAdapter(var dataSet: MutableList<WinningHistory>) :
    RecyclerView.Adapter<HistoryAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val kLbl: TextView = view.findViewById(R.id.kLbl)
        val fLbl: TextView = view.findViewById(R.id.fLbl)
        val dLbl: TextView = view.findViewById(R.id.dLbl)
        val sLbl: TextView = view.findViewById(R.id.sLbl)
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(viewGroup.context).inflate(R.layout.item_history, viewGroup, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        val kHistory = dataSet[0]
        val fHistory = dataSet[1]
        val dHistory = dataSet[2]
        val sHistory = dataSet[3]

        when (position) {
            0 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.rank1Digit4)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.rank1Digit4)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.rank1Digit4)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.rank1Digit4)
            }
            1 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.rank2Digit4)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.rank2Digit4)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.rank2Digit4)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.rank2Digit4)
            }
            2 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.rank3Digit4)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.rank3Digit4)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.rank3Digit4)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.rank3Digit4)
            }
            3 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.rank1Digit3)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.rank1Digit3)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.rank1Digit3)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.rank1Digit3)
            }
            4 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.rank2Digit3)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.rank2Digit3)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.rank2Digit3)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.rank2Digit3)
            }
            5 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.rank3Digit3)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.rank3Digit3)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.rank3Digit3)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.rank3Digit3)
            }
            6 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.rank1Digit2)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.rank1Digit2)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.rank1Digit2)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.rank1Digit2)
            }
            7 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.rank2Digit2)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.rank2Digit2)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.rank2Digit2)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.rank2Digit2)
            }
            8 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.rank3Digit2)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.rank3Digit2)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.rank3Digit2)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.rank3Digit2)
            }
            9 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.grandDigit4)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.grandDigit4)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.grandDigit4)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.grandDigit4)
            }
            10 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.grandDigit3)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.grandDigit3)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.grandDigit3)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.grandDigit3)
            }
            11 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.grandDigit2)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.grandDigit2)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.grandDigit2)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.grandDigit2)
            }
            12 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.consolePrize)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.consolePrize)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.consolePrize)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.consolePrize)
            }
            13 -> {
                viewHolder.kLbl.text = String.format("%.2f", kHistory.totalPrize)
                viewHolder.fLbl.text = String.format("%.2f", fHistory.totalPrize)
                viewHolder.dLbl.text = String.format("%.2f", dHistory.totalPrize)
                viewHolder.sLbl.text = String.format("%.2f", sHistory.totalPrize)
            }
        }
    }

    override fun getItemCount() = 14

    fun updateAdapter(newList: MutableList<WinningHistory>) {
        dataSet.clear()
        dataSet.addAll(newList)
        notifyDataSetChanged()
    }
}